package DMSXL._2024.DD_stack;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class dailyTemperatures {
    public static void main(String[] args) {
        int[] temperatures = {73,74,75,71,69,72,76,73};
        int[] result = new dailyTemperatures().dailyTemperatures(temperatures);
        for (int i : result){
            System.out.print(i + "  ");
        }
    }
    public int[] dailyTemperatures(int[] temperatures) {
      int[] result = new int[temperatures.length];
      Stack<Integer> stack = new Stack<>();
      for (int i = 0; i < temperatures.length; i++) {
          if(stack.isEmpty()){
              stack.push(i);
          }else {
              if(temperatures[i] > temperatures[stack.peek()]){
                  while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]){
                      result[stack.peek()] = i - stack.peek();
                      stack.pop();
                  }
              }
              stack.push(i);
          }
      }
        return result;
    }
}
